package org.example.reemploymentsys.controller;

import org.example.reemploymentsys.entity.Organization;
import org.example.reemploymentsys.entity.Student;
import org.example.reemploymentsys.repository.OrganizationRe;
import org.example.reemploymentsys.repository.StudentRe;
import org.example.reemploymentsys.repository.UserTbRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentAdminController {
    @Autowired
    private UserTbRe userTbRe;
    @Autowired
    private StudentRe studentRe;
    @Autowired
    private OrganizationRe organizationRe;
    @GetMapping("/getAllStudents/{username}")
    public List<Student> getAllStudents(
            @PathVariable String username,
            @RequestParam(required = false) String search)
    {
        System.out.println(username);
        System.out.println(search);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(username);
        if(search == null) {
            System.out.println(studentRe.findAllByOrganizationId(OrganizationId));
            return studentRe.findAllByOrganizationId(OrganizationId);
        }else {
            return studentRe.findAllByOrganizationIdAndNameContaining(OrganizationId, search);
        }
    }
    @PostMapping("/adminAddStudents/{username}")
    public String addStudents(
            @PathVariable String username,
            @RequestBody Student student)
    {
        System.out.println(username);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(username);
        Organization organization = organizationRe.findOrganizationById(OrganizationId);
        student.setOrganization(organization);
        studentRe.save(student);
        return "success";
    }
    @PostMapping("/adminUpdateStudents/{id}")
    public String updateStudents(
            @PathVariable Long id,
            @RequestBody Student student)
    {
        studentRe.save(student);
        return "success";
    }
    @PostMapping("/adminDeleteStudent/{id}")
    public String deleteStudent(
            @PathVariable Long id)
    {
        studentRe.deleteById(id);
        return "success";
    }
    @PostMapping("/batchDeleteStudents/{idS}")
    public String batchDeleteStudents(
             @PathVariable List<Long> idS) {
        studentRe.deleteAllById(idS);
        return "success";
    }
}
